﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using FileLibrary.Models;
using Sunny.UI;

namespace AutoShuntingSchedule.UI.Controls
{
    public partial class FDataGridView : UIPage
    {
        DataTable dataTable = new DataTable("DataTable");
        private string _models;
        private Type _value;
        private List<FileModel> dataList;
        private List<PropertyInfo> propertyInfoList;

        public FDataGridView()
        {
            InitializeComponent();

            //uiDataGridView1.DataSource = dataTable;

            //不自动生成列
            uiDataGridView1.AutoGenerateColumns = true;

            //设置分页控件每页数量
            uiPagination1.PageSize = 20;
            uiDataGridView1.SelectIndexChange += uiDataGridView1_SelectIndexChange;

            //设置统计绑定的表格
            uiDataGridViewFooter1.DataGridView = uiDataGridView1;
            uiDataGridViewFooter1.Visible = false;
        }

        public override void Init()
        {
            base.Init();
            uiPagination1.ActivePage = 1;
        }

        /// <summary>
        /// 分页控件页面切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pagingSource"></param>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            //未连接数据库，通过模拟数据来实现
            //一般通过ORM的分页去取数据来填充
            //pageIndex：第几页，和界面对应，从1开始，取数据可能要用pageIndex - 1
            //count：单页数据量，也就是PageSize值

            dataTable.Rows.Clear();
            for (int i = (pageIndex - 1) * count; i < pageIndex * count; i++)
            {
                if (dataList != null && i >= dataList.Count) break;
                if (dataList != null) dataTable.Rows.Add(GetFileModelObjList(dataList[i]).ToArray());
            }

            uiDataGridViewFooter1.Clear();
            uiDataGridViewFooter1["统计"] = $"本表格共{dataList}条数据";
        }

        private void uiDataGridView1_SelectIndexChange(object sender, int index)
        {
            index.WriteConsole("SelectedIndex");
        }

        public void SetDataSource(string models, Type value, List<FileModel> fileModels)
        {
            uiDataGridView1.DataSource = null;
            dataTable.Columns.Clear();
            dataTable.Rows.Clear();
            //Thread.Sleep(100);
            _models = models;
            _value = value;
            dataList = fileModels;

            if (fileModels != null && fileModels.Count > 0)
            {
                var fileModel = fileModels[0];
                Assembly asm = Assembly.GetExecutingAssembly();
                Object obj = asm.CreateInstance($"{value.FullName}", false);
                PropertyInfo[] fields = fileModel.GetType().GetProperties(); //获取指定对象的所有公共属性
                propertyInfoList = new List<PropertyInfo>();
                foreach (var propertyInfo in fields)
                {
                    foreach (var attribute in propertyInfo.GetCustomAttributes(false))
                    {
                        var des = attribute as DescriptionAttribute;
                        if (des == null) continue;
                        if (des.Description == "序号") continue;
                        dataTable.Columns.Add(des.Description);
                        propertyInfoList.Add(propertyInfo);
                    }
                }

                //foreach (var model in fileModels)
                //{
                //    var objList = GetFileModelObjList(model);
                //    dataTable.Rows.Add(objList.ToArray());
                //}
            }

            uiDataGridView1.DataSource = dataTable;
            //设置分页控件总数
            uiPagination1.TotalCount = dataList.Count;
        }

        private List<object> GetFileModelObjList(FileModel model)
        {
            var objList = new List<object>();
            foreach (var propertyInfo in propertyInfoList)
            {
                var modelValue = propertyInfo.GetValue(model);
                objList.Add(modelValue);
            }

            return objList;
        }
    }
}
